package designMode.statePattern;

/**
 * @author LIBOFEI
 * @ClassName GumballMachine
 * @description 糖果机
 * @date 2020/6/28 15:29
 * @Version 1.0
 **/
public class GumballMachine {

    final static int SOLD_OUT = 0;
    final static int NO_QUARTER = 1;
    final static int HAS_QUARTER = 2;
    final static int SOLD = 3;

    /** *
     * 一开始被设置为售完
     * @author LIBOFEI
     * @date 15:31 2020/6/28
     * @return
     * @Version  1.0
    **/
    int state = SOLD_OUT;

    /** *
     * 糖果数量
    **/
    int count;

    public GumballMachine(int count){
        this.count = count;
        if(count > 0){
            state = NO_QUARTER;
        }
    }

    /** *
     * 投币操作
     * @author LIBOFEI
     * @date 15:32 2020/6/28
     * @return void
     * @Version  1.0
    **/
    public void insertQuarter(){
        if(state == HAS_QUARTER){
            System.out.println("您已经投过币");
        }else if(state == NO_QUARTER){
            state = HAS_QUARTER;
            System.out.println("投币成功");
        }else if(state == SOLD_OUT){
            System.out.println("已售完");
        }else if(state == SOLD){
            System.out.println("您已购买糖果");
        }
    }

    /**
     * 退币操作
     * @author LIBOFEI
     * @date 15:36 2020/6/28
     * @return void
     * @Version  1.0
    **/
    public void ejectQuarter(){
        if(state == HAS_QUARTER){
            System.out.println("为您退币中");
            state = NO_QUARTER;
        }else if(state == NO_QUARTER){
            System.out.println("您还没有投币");
        }else if(state == SOLD_OUT){
            System.out.println("糖果已售完，不能退币");
        }else if(state == SOLD){
            System.out.println("您已拿到糖果，无法退币");
        }
    }

    /** *
     * 转动曲柄 取糖果操作
     * @author LIBOFEI
     * @date 15:38 2020/6/28
     * @return void
     * @Version  1.0
    **/
    public void turnCrank(){
        if(state == HAS_QUARTER){
            System.out.println("稍等..为您发送糖果");
            state = SOLD;
            dispense();
        }else if(state == NO_QUARTER){
            System.out.println("您还没有投币");
        }else if(state == SOLD_OUT){
            System.out.println("糖果已售完");
        }else if(state == SOLD){
            System.out.println("您已拿到糖果");
        }
    }

    /** *
     * 发放糖果
     * @author LIBOFEI
     * @date 15:41 2020/6/28
     * @return void
     * @Version  1.0
    **/
    private void dispense(){
        if(state == HAS_QUARTER){
            System.out.println("发生错误");
        }else if(state == NO_QUARTER){
            System.out.println("您还没有投币");
        }else if(state == SOLD_OUT){
            System.out.println("发生错误");
        }else if(state == SOLD){
            System.out.println("请稍等，正在取出糖果");
            count--;
            if(count == 0){
                System.out.println("糖果已售完");
                state = SOLD_OUT;
            }else{
                state = NO_QUARTER;
            }
        }
    }
}
